<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * books actions.
 *
 * @package    FinalsClub
 * @subpackage search
 * @author     Masumi
 */
class booksActions extends sfActions {
	public function executeShowTOC(sfWebRequest $request) {
		if(!$id = $request->getParameter('id')) {
			return;
		}
		$this->book = BookPeer::retrieveByPK($id);
		//$this->sections = SectionPeer::retrieveByBookId($id);
		$this->sections = SectionPeer::retrieveByBookIdInOrder($id);
	}

	public function executeShowEssay(sfWebRequest $request) {
		if(!$id = $request->getParameter('id')) {
			return;
		}
		$this->book = BookPeer::retrieveByPK($id);
		//$this->sections = SectionPeer::retrieveByBookId($id);
		$this->sections = SectionPeer::retrieveByBookIdInOrder($id);
	}

	public function executeShowSection(sfWebRequest $request) {
		if(!$id = $request->getParameter('id')) {
			return;
		}
		if(!$bookid = $request->getParameter('bookid')) {
			return;
		}
		$this->book = BookPeer::retrieveByPK($bookid);
		$this->section = SectionPeer::retrieveByPK($id);
		//$this->children = SectionPeer::getChildren($id);
		$this->content = ContentPeer::retrieveBySectionId($id);
		$this->sections = SectionPeer::retrieveByBookIdInOrder($bookid);

		$this->show_annotations = ($this->getUser()->isAuthenticated());
		if($this->show_annotations) {
			$this->getResponse()->addJavascript('/js/annotate.js', 'last');
			$this->getResponse()->addJavascript('/fckeditor/fckeditor.js', 'last');
			$annotate_api = new AnnotateApi();
			$annotate_base_url = $annotate_api->getBaseURL();

			$owner_id = $this->book->getUserId();
			$user_id = $this->getUser()->getId();
			$this->annotate_isowner = ($owner_id == $user_id);

			if ($owner_id) {
			    $owner = UserPeer::retrieveById($owner_id);
			    $owner_email = $owner->getEmail();
			    $owner_sig = $owner->getUsername();
			}
			else {
			    $owner_email = "books@finalsclub.org";//sfConfig::get('app_fcbooks_owner');
			    $owner_sig = "books";
			}

			$this->annotate_owner = $annotate_api->encodeEmail($owner_email);
			$this->annotate_owner_sig = $owner_sig;

			$user = UserPeer::retrieveById($user_id);
			$this->annotate_user_sig = $user->getUsername();

			$this->annotate_title = "FinalsClub.com Books - ".$this->book->getTitle()." :: ".$this->section->getName();
			$this->getResponse()->addJavascript($annotate_base_url . '/js/web_annotate.js', 'last'); // this is part of the a.nnotate distribution
		}
	}
}
